package com.platform.controller;

import cn.hutool.core.date.DateUtil;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.platform.common.Result;
import com.platform.entity.Classify;
import com.platform.entity.SysUser;
import com.platform.service.ClassifyService;
import com.platform.service.SysUserService;
import com.platform.entity.Product;
import com.platform.service.ProductService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/product")
public class ProductController {
    @Resource
    private ProductService productService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private SysUserService userService;
    @Resource
    private ClassifyService classifyService;
    public SysUser getUser() {
        String token = request.getHeader("token");
        String username = JWT.decode(token).getAudience().get(0);
        return userService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username));
    }

    @PostMapping
    public Result<?> save(@RequestBody Product goods) {
        goods.setCreateTime(DateUtil.now());
        productService.save(goods);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Product goods) {
        productService.updateById(goods);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        productService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> findById(@PathVariable Long id) {
        Product goods = productService.getById(id);
        goods.setRealPrice(goods.getPrice().multiply(BigDecimal.valueOf(goods.getDiscount())));
        return Result.success(goods);
    }

    @GetMapping
    public Result<?> findAll() {

        List<Product> list = productService.list();
        list.forEach(v->{
            Classify classify = classifyService.getById(v.getCategoryId());
            v.setCategoryName(classify.getName());
        });
        return Result.success(list);
    }

    /**
     * 推荐商品
     * @return
     */
    @GetMapping("/recommend")
    public Result<?> recommend() {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
//        wrapper.last("limit 3");
        wrapper.eq(Product::getRecommend,true);
        wrapper.last("limit 4");

        List<Product> list = productService.list(wrapper);
        return Result.success(list);
    }

    /**
     * 推热销商品
     * @return
     */
    @GetMapping("/sales")
    public Result<?> sales() {
        List<Product> list = productService.sales();
        return Result.success(list);
    }

    /**
     * 根据分类id查询商品
     * @param id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/byCategory/{id}")
    public Result<?> findByCategory(@PathVariable Long id,
                                    @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                    @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        IPage<Product> page = productService.pageByCategory(new Page<>(pageNum, pageSize), id);
        return Result.success(page);
    }

    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(required = false, defaultValue = "") String name,
                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Product::getName,name);
        IPage<Product> page = productService.page(new Page<>(pageNum, pageSize), wrapper);
        page.getRecords().forEach(v->{
            Classify classify = classifyService.getById(v.getCategoryId());
            v.setCategoryName(classify.getName());
        });
        return Result.success(page);
    }

}
